/*
	- Partial R-squared
	- December sentiment
	- Welfare-relevant productivity
	- Sensitivity analysis
	- Foreign direct investments
	- Real GDP growth forecasts
*/




*********************
* Partial R-squared *
*********************

clear all
use "data/final_data_set.dta"
xtset id year

* Table_02 TFP growth and raw sentiment, local + US macro variables: full sample
	// with sentiment
		foreach x in f_rtfpna f2_rtfpna f3_rtfpna f4_rtfpna {
			qui: regxfe `x' zcci zd_* zF*, robust fe(id) cluster(year)
				eststo
		}
		esttab , cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_02) ///
		t starlevels(* .10 ** .05 *** .01) b(4) scalars(r2)     ///
		drop()
		estimates clear
	// without sentiment
		foreach x in f_rtfpna f2_rtfpna f3_rtfpna f4_rtfpna {
			qui: regxfe `x' zd_* zF*, robust fe(id) cluster(year)
				eststo
		}
		esttab , cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_02) ///
		t starlevels(* .10 ** .05 *** .01) b(4) scalars(r2)     ///
		drop()
		estimates clear

/* comparison of within R-squared (option: "scalars(r2)" instead of "scalars(r2o)")

- full sample
	0.4248          0.4083          0.3945          0.3772  
	0.3428          0.3461          0.3414          0.3323 
- G7 countries
	0.4412          0.4299          0.4188          0.3998
	0.4422          0.4355          0.4244          0.4051   
- non-G7 countries
	0.4826          0.4562          0.4448          0.4238 
	0.3363          0.3425          0.3382          0.3282   

for overall R-squared it makes little difference because it is mainly driven by FE	
*/		



**********************
* December sentiment *
**********************

	clear all
	use "data/final_data_set.dta"
	xtset id year

* List of countries
	tabulate country if zcci_m12 != . & rtfpna != .
	// for reference, OECD members are here: https://data.worldbank.org/country/OE
	

* Table_Dec02 TFP growth and raw sentiment, local + US macro variables: full sample
	foreach x in f_rtfpna f2_rtfpna f3_rtfpna f4_rtfpna {
		qui: regxfe `x' zcci_m12 zd_* zF*, robust fe(id) cluster(year)
			eststo
	}
	esttab using "Table_Dec02.rtf", replace cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_Dec02) ///
	t starlevels(* .10 ** .05 *** .01) b(4) scalars(r2o)     ///
	drop()
	estimates clear

	
* Table_Dec03 TFP growth and raw sentiment, local + US macro variables: G7 countries
	foreach x in f_rtfpna f2_rtfpna f3_rtfpna f4_rtfpna {
		qui: regxfe `x' zcci_m12 zd_* zF* if dum_G7 == 1, robust fe(id) cluster(year)
			eststo
	}
	esttab using "Table_Dec03.rtf", replace cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_Dec03) ///
	t starlevels(* .10 ** .05 *** .01) b(4) scalars(r2o)     ///
	drop()
	estimates clear


* Table_Dec04 TFP growth and raw sentiment, local + US macro variables: G7 countries
	foreach x in f_rtfpna f2_rtfpna f3_rtfpna f4_rtfpna {
		qui: regxfe `x' zcci_m12 zd_* zF* if dum_G7 == 0, robust fe(id) cluster(year)
			eststo
	}
	esttab using "Table_Dec04.rtf", replace cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_Dec04) ///
	t starlevels(* .10 ** .05 *** .01) b(4) scalars(r2o)     ///
	drop()
	estimates clear

	
	
*********************************
* Welfare-relevant productivity *
*********************************

	clear all
	use "data/final_data_set.dta"
	xtset id year

* List of countries
	tabulate country if zcci != . & rwtfpna != .
	// for reference, OECD members are here: https://data.worldbank.org/country/OE
	

* Table_w02 TFP growth and raw sentiment, local + US macro variables: full sample
	foreach x in f_rwtfpna f2_rwtfpna f3_rwtfpna f4_rwtfpna {
		qui: regxfe `x' zcci zd_* zF*, robust fe(id) cluster(year)
			eststo
	}
	esttab using "Table_w03.rtf", replace cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_w03) ///
	t starlevels(* .10 ** .05 *** .01) b(4) scalars(r2o)     ///
	drop()
	estimates clear
	
* Table_w03 TFP growth and raw sentiment, local + US macro variables: G7 countries
	foreach x in f_rwtfpna f2_rwtfpna f3_rwtfpna f4_rwtfpna {
		qui: regxfe `x' zcci zd_* zF* if dum_G7 == 1, robust fe(id) cluster(year)
			eststo
	}
	esttab using "Table_w03.rtf", replace cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_w03) ///
	t starlevels(* .10 ** .05 *** .01) b(4) scalars(r2o)     ///
	drop()
	estimates clear


* Table_w04 TFP growth and raw sentiment, local + US macro variables: G7 countries
	foreach x in f_rwtfpna f2_rwtfpna f3_rwtfpna f4_rwtfpna {
		qui: regxfe `x' zcci zd_* zF* if dum_G7 == 0, robust fe(id) cluster(year)
			eststo
	}
	esttab using "Table_w04.rtf", replace cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_w04) ///
	t starlevels(* .10 ** .05 *** .01) b(4) scalars(r2o)     ///
	drop()
	estimates clear

	
	
************************
* Sensitivity analysis *
************************

clear all
use "data/final_data_set.dta"
xtset id year

			
* Permutations
	// G7: ITA DEU GBR USA FRA CAN
		// results hold for all except when leaving out the USA (coefficient becomes negative)

		foreach x in f_rtfpna f2_rtfpna f3_rtfpna f4_rtfpna {
			qui: regxfe `x' zcci zd_* zF* if cou != "CAN" & dum_G7 == 1, robust fe(id) cluster(year)
				eststo
		}
		esttab , cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_02) ///
		t starlevels(* .10 ** .05 *** .01) b(4) scalars(r2)     ///
		drop()
		estimates clear

	// non-G7: AUS AUT BEL CHE DNK FIN IRL ESP NLD NZL SWE
		// results hold for all except (and only partly) when leaving out BEL and ESP

		foreach y in AUS AUT BEL CHE DNK FIN IRL ESP NLD NZL SWE {
		foreach x in f_rtfpna f2_rtfpna f3_rtfpna f4_rtfpna {
			qui: regxfe `x' zcci zd_* zF* if cou != "`y'" & dum_G7 == 0, robust fe(id) cluster(year)
				eststo
		}
		esttab , keep(zcci) cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_02) ///
		t starlevels(* .10 ** .05 *** .01) b(4) scalars(r2)     ///
		drop()
		estimates clear
		}
		

		
******************************
* Foreign direct investments *
******************************

* Import inward FDIs
	// import
		clear all
		import excel "orig/world_bank/FDIs_inflows.xlsx", sheet("final_series") firstrow
	// transform into panel format	
		reshape long ifdi, i(cou) j(year) string	
		sort cou year
		destring year, replace
	// check out distribution
		tabstat ifdi, statistics(min p1 p5 p25 p50 p75 p95 p99 max)
	// winsorize
		winsor2 ifdi, cuts(1 99)
	// take log of *rank* of FDIs
		gen ln_ifdi = ln(1 + ifdi + 3.30e+11)
		tabstat ln_ifdi, statistics(min p1 p5 p25 p50 p75 p95 p99 max)
	// save
		save "data/FDIs_inflows.dta", replace	

* Import outward FDIs
	// import
		clear all
		import excel "orig/world_bank/FDIs_outflows.xlsx", sheet("final_series") firstrow
	// transform into panel format	
		reshape long ofdi, i(cou) j(year) string	
		sort cou year
		destring year, replace
	// check out distribution
		tabstat ofdi, statistics(min p1 p5 p25 p50 p75 p95 p99 max)
	// winsorize
		winsor2 ofdi, cuts(1 99)
	// take log of *rank* of FDIs
		gen ln_ofdi = ln(1 + ofdi + 87.23086)
		tabstat ln_ofdi, statistics(min p1 p5 p25 p50 p75 p95 p99 max)
	// save
		save "data/FDIs_outflows.dta", replace	

* Merge with main data
	clear all
	use "data/final_data_set.dta"
	xtset id year
	foreach x in FDIs_inflows FDIs_outflows ///
	{
		merge m:m cou year using "data/`x'.dta", keep(match master)
		drop _merge
	}
	// delete individual series
		foreach x in FDIs_inflows FDIs_outflows ///
		{
			erase "data/`x'.dta"
		}		
	// create net inflow
		gen nfdi = ofdi - ifdi
	// check coverage
		tabulate country if nfdi != . & zcci_ort != .
		tabstat year if nfdi != . & zcci_ort != ., statistics(min max)
	// standardize and winsorize variables
		foreach x in ifdi ofdi nfdi {
			egen `x'mean = mean(`x')
			egen `x'sd = sd(`x')
			gen z`x' = (`x'-`x'mean) / `x'sd
			drop `x'sd `x'mean
			winsor2 z`x', cuts(1 99) replace
		}
			
	tabstat *fdi*, statistics(min p1 p5 p10 p25 p50 p75 p90 p95 p99 max)

	// calculate future values
		xtset id year
		foreach x in zifdi zofdi znfdi {
			gen f_`x' = f.`x'
			gen f2_`x' = f2.`x'
			gen f3_`x' = f3.`x'
			gen f4_`x' = f4.`x'
			gen f5_`x' = f5.`x'
			gen f6_`x' = f6.`x'
			gen f7_`x' = f7.`x'
			gen f8_`x' = f8.`x'				
		}

	generate newid = id
	tsset newid year
	
* Inward FDIs
// full sample
	foreach x in f_zifdi f2_zifdi f3_zifdi f4_zifdi {
			qui: bootstrap, rep(200) seed(123) cluster(id) idcluster(newid) nodots nowarn: ///
			xtreg `x' zcci_ort zcci_exp i.year, fe
				eststo
		}
	esttab using "Table_FDIs_inward_a.rtf", replace cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_FDIs_inward) ///
	t starlevels(* .10 ** .05 *** .01) b(4) scalars(r2)     ///
	drop()
	estimates clear
// G7 countries
	foreach x in f_zifdi f2_zifdi f3_zifdi f4_zifdi {
			qui: bootstrap, rep(200) seed(123) cluster(id) idcluster(newid) nodots nowarn: ///
			xtreg `x' zcci_ort zcci_exp i.year if dum_G7 == 1, fe
				eststo
		}
	esttab using "Table_FDIs_inward_b.rtf", replace cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_FDIs_inward) ///
	t starlevels(* .10 ** .05 *** .01) b(4) scalars(r2)     ///
	drop()
	estimates clear
// non-G7 countries
	foreach x in f_zifdi f2_zifdi f3_zifdi f4_zifdi {
			qui: bootstrap, rep(200) seed(123) cluster(id) idcluster(newid) nodots nowarn: ///
			xtreg `x' zcci_ort zcci_exp i.year if dum_G7 == 0, fe
				eststo
		}
	esttab using "Table_FDIs_inward_c.rtf", replace cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_FDIs_inward) ///
	t starlevels(* .10 ** .05 *** .01) b(4) scalars(r2)     ///
	drop()
	estimates clear

* Outward FDIs
// full sample
	foreach x in f_zofdi f2_zofdi f3_zofdi f4_zofdi {
			qui: bootstrap, rep(200) seed(123) cluster(id) idcluster(newid) nodots nowarn: ///
			xtreg `x' zcci_ort zcci_exp i.year, fe
				eststo
		}
	esttab using "Table_FDIs_outward_a.rtf", replace cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_FDIs_outward) ///
	t starlevels(* .10 ** .05 *** .01) b(4) scalars(r2)     ///
	drop()
	estimates clear
// G7 countries
	foreach x in f_zofdi f2_zofdi f3_zofdi f4_zofdi {
			qui: bootstrap, rep(200) seed(123) cluster(id) idcluster(newid) nodots nowarn: ///
			xtreg `x' zcci_ort zcci_exp i.year if dum_G7 == 1, fe
				eststo
		}
	esttab using "Table_FDIs_outward_b.rtf", replace cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_FDIs_outward) ///
	t starlevels(* .10 ** .05 *** .01) b(4) scalars(r2)     ///
	drop()
	estimates clear
// non-G7 countries
	foreach x in f_zofdi f2_zofdi f3_zofdi f4_zofdi {
			qui: bootstrap, rep(200) seed(123) cluster(id) idcluster(newid) nodots nowarn: ///
			xtreg `x' zcci_ort zcci_exp i.year if dum_G7 == 0, fe
				eststo
		}
	esttab using "Table_FDIs_outward_c.rtf", replace cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_FDIs_outward) ///
	t starlevels(* .10 ** .05 *** .01) b(4) scalars(r2)     ///
	drop()
	estimates clear
	

	
*****************************
* Real GDP growth forecasts *
*****************************
		
// import data from Excel to Stata and set time
	foreach x in ltgdp_f g_demand_f g_rgdp_f ///
	{
		clear all
		import excel "orig/OECD/`x'.xlsx", sheet("final_series") firstrow
		destring year, replace
		save "data/`x'_a.dta", replace
	}		
// merge
	foreach x in ltgdp_f g_demand_f g_rgdp_f ///
	{
		merge m:m cou year using "data/`x'_a.dta", keep(match master)
		drop _merge
	}
// express growth rates (demand and rgdp forecasts) in decimals	
	foreach x in g_demand_f g_rgdp_f {
		gen `x'100 = `x' / 100
		drop `x'
		rename `x'100 `x'
	}		
		save "data/forecasts_data.dta", replace
// delete individual series
	foreach x in ltgdp_f g_demand_f g_rgdp_f ///
	{
		erase "data/`x'_a.dta"
	}
	
	clear all
	use "data/final_data_set.dta"
	xtset id year
	merge m:m cou year using "data/forecasts_data.dta", keep(match master)
	drop _merge
	xtset id year

	tabstat g_*, statistics(min mean sd med max)

// data availability per country
	tabulate country if g_demand_f != . & zcci_ort != .
// sample period availability
	tabstat year if g_demand_f != . & zcci_ort != ., statistics(min max)
	
// calculate future values
	foreach x in ltgdp_f g_demand_f g_rgdp_f  {
		gen f_`x' = f.`x'
		gen f2_`x' = f2.`x'
		gen f3_`x' = f3.`x'
		gen f4_`x' = f4.`x'
		gen f5_`x' = f5.`x'
		gen f6_`x' = f6.`x'
		gen f7_`x' = f7.`x'
		gen f8_`x' = f8.`x'				
	}	

generate newid = id
tsset newid year
		
// full sample
	foreach x in f_g_rgdp_f f2_g_rgdp_f f3_g_rgdp_f f4_g_rgdp_f {
			qui: bootstrap, rep(200) seed(123) cluster(id) idcluster(newid) nodots nowarn: ///
			xtreg `x' zcci_ort zcci_exp i.year, fe
				eststo
		}
	esttab using "Table_ergdp_a.rtf", replace cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_ergdp) ///
	t starlevels(* .10 ** .05 *** .01) b(4) scalars(r2)     ///
	drop()
	estimates clear
// G7 countries
	foreach x in f_g_rgdp_f f2_g_rgdp_f f3_g_rgdp_f f4_g_rgdp_f {
			qui: bootstrap, rep(200) seed(123) cluster(id) idcluster(newid) nodots nowarn: ///
			xtreg `x' zcci_ort zcci_exp i.year if dum_G7 == 1, fe
				eststo
		}
	esttab using "Table_ergdp_b.rtf", replace cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_ergdp) ///
	t starlevels(* .10 ** .05 *** .01) b(4) scalars(r2)     ///
	drop()
	estimates clear
// non-G7 countries
	foreach x in f_g_rgdp_f f2_g_rgdp_f f3_g_rgdp_f f4_g_rgdp_f {
			qui: bootstrap, rep(200) seed(123) cluster(id) idcluster(newid) nodots nowarn: ///
			xtreg `x' zcci_ort zcci_exp i.year if dum_G7 == 0, fe
				eststo
		}
	esttab using "Table_ergdp_c.rtf", replace cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_ergdp) ///
	t starlevels(* .10 ** .05 *** .01) b(4) scalars(r2)     ///
	drop()
	estimates clear
							
							
